package com.suxin.socket.simple;

import com.suxin.socket.simple.domain.User;
import org.springframework.stereotype.Component;

import javax.validation.constraints.NotNull;
import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;

/**
 * @author Tang
 * @classname SimpleWebSocket
 * @description [  ]
 * @date 2021/9/26 15:02
 */
@Component
@ServerEndpoint(value = "/simple/{id}")
public class SimpleWebSocket {

    private User user;

    @OnOpen
    public void open(@NotNull @PathParam("id") String id,Session session) {
        user = new User(Integer.parseInt(id));
    }

    @OnMessage
    public void message(String message,Session session) {
        System.out.println(this.user + "-->" + message);
    }

    @OnClose
    public void close(Session session) {
        try {
            session.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @OnError
    public void error(Session session,Throwable e) {
        System.out.println(e.getMessage());
    }


}